//
//  MCFunction.m
//  MClient
//
//  Created by  史正烨 on 09-10-23.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MCFunction.h"
#import "MClientAppDelegate.h"
#import "MServerDAO.h"
#import "EditFormController.h"
#import "ViewFormController.h"
#import "SelectFieldViewController.h"
#import "MCUrlProcess.h"
#import "FileListViewController.h"
#import "TextFieldViewController.h"
//#import "LocationView.h"
#import "FieldAction.h"
#import "FileViewController.h"
#import "NSData+Base64.h"
#include <sys/types.h>
#include <sys/sysctl.h>
#import "AListLookupController.h"
#import "TreeLookupController.h"
#import "ListController.h"
#import "AListController.h"
#import "LookupController.h"
#import "CustomFormController.h"
#import "MCUIToolkit.h"
#import "MCControl.h"
#import "DateTimeSelectView.h"
#import "MCTableCell.h"
#import "CustomLookupController.h"
#import "CustomAListLookupController.h"
#import "MCImageView.h"
#import "ZipArchive.h"
#import <QuartzCore/CALayer.h>
#import "LookupSelectView.h"
#import "MCSwitch.h"
#import "UILabelAdditions.h"
#import "ProductViewAddition.h"
#import "RarOrZipController.h"
#import "EGOPhotoViewController.h"

@implementation MCFunction

+ (void)pushViewByUrl:(NSDictionary *)url 
		   parameters:(NSMutableDictionary *)parameters 
	   confirmMessage:(NSString *)confirmMessage
	   naviController:(UINavigationController*)naviController{
	MCUrlProcess *mcUrlProcess = [MCUrlProcess sharedInstanceWithUrl:url 
														  parameters:parameters 
													  confirmMessage:confirmMessage
													  naviController:naviController];
	[mcUrlProcess doUrlProcess];
	//[mcUrlProcess release];
}

+ (void)replaceAndGo:(NSMutableDictionary *)url 
		  parameters:(NSMutableDictionary *)parameters 
	  confirmMessage:(NSString *)confirmMessage 
	  naviController:(UINavigationController*)naviController{
	UIViewController *viewController = [naviController topViewController];
	if ([viewController isMemberOfClass:[EditFormController class]]|| 
		[viewController isMemberOfClass:[CustomFormController class]]) {
		//[naviController popViewControllerAnimated:NO];
		viewController.view.tag = -10000;
	}	
	[MCFunction pushViewByUrl:url parameters:parameters confirmMessage:confirmMessage naviController:naviController];
}

+ (Boolean)isEditForm:(UIViewController*)viewController {
	if ([viewController isMemberOfClass:[[[MCUserDefaults instance] class:@"EditFormController"] class]] || 
		[viewController isMemberOfClass:[[[MCUserDefaults instance] class:@"CustomFormController"] class]]) {
		return YES;
	}
	return NO;
}

+ (void)refreshAndGoParentView:(UIViewController*)viewController  {	
	UIViewController *preController = nil;
	for (UIViewController* controller in viewController.navigationController.viewControllers) {
		if (controller == viewController) {
			if ([preController isMemberOfClass:[[[MCUserDefaults instance] class:@"ViewFormController"] class]]) {
				[(ViewFormController *)preController getDataFromServer];
				[viewController.navigationController popViewControllerAnimated:YES];
				return;
			}
			else if ([preController isMemberOfClass:[[[MCUserDefaults instance] class:@"ListController"] class]]) {
				((ListController *)preController).dataRefresh = YES;
				[(ListController *)preController getDataFromServer];
				[viewController.navigationController popViewControllerAnimated:YES];
				return;
			}
			else if ([preController isMemberOfClass:[[[MCUserDefaults instance] class:@"AListController"] class]]){
				((AListController *)preController).dataRefresh = YES;
				[(AListController *)preController getDataFromServer];
				[viewController.navigationController popViewControllerAnimated:YES];
				return;
			}
		}
		preController = controller;
	}
	[viewController.navigationController popViewControllerAnimated:YES];
}

+ (void)refreshCurrentView:(UIViewController*)viewController {
	
	if ([viewController isMemberOfClass:[[[MCUserDefaults instance] class:@"ViewFormController"] class]]) {
		[(ViewFormController *)viewController getDataFromServer];
	}
	else if ([viewController isMemberOfClass:[[[MCUserDefaults instance] class:@"ListController"] class]]) {
		((ListController *)viewController).dataRefresh = YES;
		[(ListController *)viewController getDataFromServer];
	}
	else if ([viewController isMemberOfClass:[[[MCUserDefaults instance] class:@"AListController"] class]]){
		((AListController *)viewController).dataRefresh = YES;
		[(AListController *)viewController getDataFromServer];
	}
}

//20110922 点逸公司修改内容
+ (NSInteger) cellHeight:(int)lableStyle 
			   withTitle:(NSString *)aTitle 
			   withValue:(NSString *)aValue
			 withAppItem:(NSMutableDictionary *)aAppItem
{	
    
	float fontSize = 16;
	NSMutableDictionary *style = [aAppItem objectForKey:@"style"];
    int vline = [[[aAppItem objectForKey:@"fld"] objectForKey:@"vline"] intValue];  //huotianjun增加 vline是指行数？

	if (style != nil) {
		if ([style objectForKey:@"font_size"]) {
			fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
		}
	}
	NSString* text = [aValue stringByReplacingOccurrencesOfString:@" " withString:@"　"];
	
	if (lableStyle == 2) {//标题隐藏，内容换行
		CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:fontSize]
					   constrainedToSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds)-20, 9999)];
		int nLine = size.height/(fontSize+4); nLine = ((nLine==0)? 1:nLine);
        if (vline != 0) nLine = vline;
		return nLine*(fontSize+4)+ 22;
	} else {//if (lableStyle == 1) {//标题、内容换行
		CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] 
					   constrainedToSize:CGSizeMake(lableStyle == 1 ? 
													CGRectGetWidth([UIScreen mainScreen].bounds)-25 : 
													CGRectGetWidth([UIScreen mainScreen].bounds)-90-20, 
													9999)];
		float nLine = size.height/(fontSize+4); nLine = ((nLine<=0)? 1:nLine);
        if (vline != 0) nLine = vline;
        
		if (lableStyle == 1) {
			return MAX(size.height+25 + 25, nLine*(fontSize+4)+25 + 25);
		}
		return MAX(nLine*(fontSize+4)+25, size.height+25);
	}
}

//20110922 点逸公司修改内容
/*+ (NSInteger) cellHeight:(int)lableStyle 
			   withTitle:(NSString *)aTitle 
			   withValue:(NSString *)aValue
			 withAppItem:(NSMutableDictionary *)aAppItem
{	
	//	if (lableStyle == 1) {//标题、内容换行
	//		CGSize size = [aValue sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(300, 9999)];
	//		int nLine = size.height/20; nLine = nLine==0? 1:nLine;
	//		
	//		return nLine*20+22 + 25;
	//	} else if (lableStyle == 2) {//标题隐藏，内容换行
	//		CGSize size = [aValue sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(300, 9999)];
	//		int nLine = size.height/20; nLine = nLine==0? 1:nLine;
	//		
	//		return nLine*20+22;
	//	} else {
	//		return 44;
	//	}
	float fontSize = 16;
	NSMutableDictionary *style = [aAppItem objectForKey:@"style"];
	if (style != nil) {
		if ([style objectForKey:@"font_size"]) {
			fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
		}
	}
	NSString* text = [aValue stringByReplacingOccurrencesOfString:@" " withString:@""];
	//时间换行问题
	text = [text stringByReplacingOccurrencesOfString:@":" withString:@"："]; 
	
	if (lableStyle == 2) {//标题隐藏，内容换行
		CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:fontSize]
					   constrainedToSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds)-20, 9999)];
		int nLine = size.height/(fontSize+4); nLine = ((nLine==0)? 1:nLine);
		return nLine*(fontSize+4)+ 22;
	} else {//if (lableStyle == 1) {//标题、内容换行
		CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:16.5] 
					   constrainedToSize:CGSizeMake(lableStyle == 1 ? 
													CGRectGetWidth([UIScreen mainScreen].bounds)-25 : 
													CGRectGetWidth([UIScreen mainScreen].bounds)-90-20, 
													9999)];
		float nLine = size.height/(fontSize+4); nLine = ((nLine<=0)? 1:nLine);
		if (lableStyle == 1) {
			return MAX(size.height+25 + 25, nLine*(fontSize+4)+25 + 25);
		}
		return MAX(nLine*(fontSize+4)+25, size.height+25);
	}
}
*/
+ (void)labelColor:(UILabel*)label {
	label.textColor = [UIColor redColor];
}


#pragma mark --- 高级样式，一行多列 开始 ---

#pragma mark --- 高级样式，一行多列 编辑表单开始 ---

+(void)didSelectRowIndexPath:(UITableViewCell *)cell 
						   x:(int)x
						   y:(int)y
					   width:(int)width
					  height:(int)height 
						 tag:(int)tag
					  target:(id)target {
	UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
	button.tag = -10000 + tag;
	[button setBackgroundColor:[UIColor clearColor]];
	[button setBackgroundImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
	button.frame = CGRectMake(x + width - 14, y + (height - 28)/2, 28, 28);
	[button addTarget:target action:@selector(didSelectRowIndexPath:)
	 forControlEvents:UIControlEventTouchUpInside];
	button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin| UIViewAutoresizingFlexibleBottomMargin;
	[cell.contentView addSubview:button];
}

+ (void)addCellForEditForm:(UITableView *)tableView 
				   appItem:(NSMutableDictionary *)appItem
				 indexPath:(NSIndexPath *)indexPath
					   row:(int)row
				  formView:(UIView *)formView
					  cell:(UITableViewCell *)cell 
						 x:(int)x
						 y:(int)y 
					target:(id)target {
	int labelTag = -99 + row;
	int valueTag = -9999 + indexPath.section*100 + row;// -2
	UIView *uiview = [cell.contentView viewWithTag:labelTag];
	if(uiview) {
		return;
	}
	
	bool readFlag = [[appItem objectForKey:@"rdy"] boolValue];
	bool required = [[appItem objectForKey:@"rqd"] boolValue];
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	int width = 320 - nFormLabelLeftSpace*2, lblwidth = nFormLabelWidth, valuewidth = width;
	if (style != nil) {
		if ([style objectForKey:@"width"]) {
			width = [[style objectForKey:@"width"] intValue] * 320 / 100 - nFormLabelLeftSpace*2;// 百分比
		}
		
		if ([style objectForKey:@"lblwidth"]) {
			lblwidth = [[style objectForKey:@"lblwidth"] intValue] * 320 / 100;// 百分比
		} 
	}
	
	if(lableStyle == 1) {
		lblwidth = width;
	} else if(lableStyle == 2) {
		lblwidth = 0;
	}
	
	if(lableStyle == 0) {
		valuewidth = width - lblwidth;
	} else {
		valuewidth = width;
	}
	
	MCLabel *labelField = [[MCLabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(x+10, y, lableStyle==1?(lblwidth - 5):(lblwidth - 10), 44)];
	//MCLabel *labelField = [[MCLabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(x, 0, lableStyle==1?295:60, 44)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:16];
	labelField.numberOfLines = 1;
	labelField.tag = labelTag;
	labelField.text = [MCUIToolkit getFieldLabel:appItem];
	labelField.autoresizingMask = nTableCellAutoresizingMask;
	
	if (required) {
		[self labelColor:labelField];
		labelField.toastMessage = @"此为必填项！";
	}
	[cell.contentView addSubview:labelField];
	[labelField release];
	
	if (style != nil) {
		float fontSize = 18.0;
		if ([style objectForKey:@"label_color"]) {
			labelField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"label_color"]]];
		}
		if ([style objectForKey:@"label_size"]) {
			fontSize = [[style objectForKey:@"label_size"] floatValue]/0.75;
			labelField.font = [UIFont boldSystemFontOfSize:fontSize];
		}
		if ([style objectForKey:@"label_style"]) {
			NSString *fontStyle = [style objectForKey:@"label_style"];
			if ([fontStyle isEqualToString:@"B"]) {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}else if ([fontStyle isEqualToString:@"I"]) {
				labelField.font = [UIFont italicSystemFontOfSize:fontSize];
			}else {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
		}
	}
	
	/*
	 UILabel *spaceField = [[UILabel alloc] initWithFrame:CGRectMake(lableStyle>0?8:108, lableStyle==1?38:10, 2, 22)];
	 spaceField.backgroundColor = [UIColor clearColor];
	 [cell.contentView addSubview:spaceField];
	 [spaceField release];
	 if (required) {
	 spaceField.backgroundColor = [UIColor redColor];
	 }
	 */
	
	BOOL canSelect = NO;
	if(cell.tag != -9999) {
		canSelect = YES;
	}
	
	CGRect valueRect = CGRectMake(lableStyle>0?(x + 10):(x + lblwidth + 10), lableStyle==1?(y + 40): (y + 10), valuewidth, 44);
	//CGRect valueRect = CGRectMake(lableStyle>0?10:(x+80), lableStyle==1?40:10, lableStyle>0?300:190, 999);
	BOOL bDefault = YES;
	
	switch (cFieldType) {
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.textAlignment = UITextAlignmentLeft;
			//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			
			if ([appItem objectForKey:@"style"]) {
				((MCTextField*)valueField.control).formulaStyle = [appItem objectForKey:@"style"];
			}
			valueField.tag = valueTag;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
				labelField.input = (MCTextField*)valueField.control;
			}
			if (cFieldType == 'O') {
				valueField.secureTextEntry = YES;
			} else if (cFieldType == 'A') {
				valueField.keyboardType = UIKeyboardTypeEmailAddress;
			} else if (cFieldType == '5') {
				valueField.keyboardType = UIKeyboardTypeURL;
			} else if (cFieldType == '6' || cFieldType == '7' || cFieldType == 'E') {
				valueField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
			} else if (cFieldType == '9' || cFieldType == 'Q') {
				valueField.keyboardType = UIKeyboardTypePhonePad;
			} else {
				valueField.keyboardType = UIKeyboardTypeDefault;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
		}
			break;
		case 'N': //验证码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.frame = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?200:100, 999);
			valueField.textAlignment = UITextAlignmentLeft;
			//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.keyboardType = UIKeyboardTypeDefault;
			valueField.tag = valueTag;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
				labelField.input = (MCTextField*)valueField.control;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			UIWebView *checkImage = [[UIWebView alloc] initWithFrame:CGRectMake(240, lableStyle==1?40:12, 60, 20)];
			checkImage.tag = -3;
			[cell.contentView addSubview:checkImage];
			[checkImage release];
		}
			break;
		case 'B': //复选框
		{
			MCSwitch *valueField = [[MCSwitch alloc] initWithFrame:valueRect];
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.tag = valueTag;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
		}
			break;
		case 'Y': //文件（多个）
		case '8': //文本区
		case 'G': //文本区
		case 'T': //手机定位
		case 'J': //文件
		{
			valueRect.size.width = valueRect.size.width - 22;
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
			//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.numberOfLines = 99;
			valueField.tag = valueTag;
			[cell.contentView addSubview:valueField];
			[valueField release];
			if(canSelect) {
				cell.selectionStyle = UITableViewCellSelectionStyleBlue;
				//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			}
			if (cFieldType == '8' || cFieldType == 'G') {
				if (readFlag) {
					if (lableStyle == 1 || lableStyle == 2) {
						cell.accessoryType = UITableViewCellAccessoryNone;
						cell.selectionStyle = UITableViewCellSelectionStyleNone;
					}
				} else {
					[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
				}
			} else {
				[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
			}
		}
			break;
		case '0': //html
		{	
			valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
			UIWebView* webView = [[UIWebView alloc] initWithFrame:valueRect];
			webView.scalesPageToFit = NO;
			webView.tag = valueTag;
			webView.userInteractionEnabled = NO;
			[cell.contentView addSubview:webView];
			[webView release];
		}
			break;
		case 'S': //图片
		{
			if(!readFlag) {
				valueRect.size.width = valueRect.size.width - 22;
			}
			MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
			[cell.contentView addSubview:imgView];
			imgView.tag = valueTag;
			//imgView.autoresizingMask = nTableCellAutoresizingMask;
			[imgView release];
			
			if(!readFlag) {
				if(canSelect) {
					cell.selectionStyle = UITableViewCellSelectionStyleBlue;
					//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				}
				
				[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
			}
			
			//if (readFlag) {
			//				//valueRect = CGRectMake(lableStyle>0?(x + 10):(x + lblwidth + 10), lableStyle==1?(y + 40): (y + 10), valuewidth, 44);
			//				//valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 35);
			//				MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
			//				imgView.tag = valueTag;
			//				[cell.contentView addSubview:imgView];
			//				[imgView release];
			//			} else {
			//				valueRect.size.width = valueRect.size.width - 16;
			//				MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
			//				valueField.textAlignment = UITextAlignmentLeft;
			//				//				valueField.textColor = [UIColor grayColor];
			//				valueField.backgroundColor = [UIColor clearColor];
			//				valueField.font = [UIFont boldSystemFontOfSize:16];
			//				valueField.tagEx = indexPath.section*100 +indexPath.row;
			//				valueField.fieldType = cFieldType;
			//				valueField.autoresizingMask = nTableCellAutoresizingMask;
			//				valueField.numberOfLines = 99;
			//				valueField.tag = valueTag;
			//				[cell.contentView addSubview:valueField];
			//				[valueField release];
			//				
			//				if(canSelect) {
			//					cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			//					//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			//				}
			//				
			//				[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
			//			}
		}
			break;
			
			/*
			 case '0': //html
			 {
			 valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
			 UIWebView* webView = [[UIWebView alloc] initWithFrame:valueRect];
			 webView.scalesPageToFit = NO;
			 webView.tag = -2;
			 webView.userInteractionEnabled = NO;
			 [cell.contentView addSubview:webView];
			 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			 }
			 break;
			 */
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'C': //日期
		case 'D': //日期时间
			bDefault = NO;
		case '1': //公式
		case '2': //自动编号
		default:
		{
			if (!(readFlag || bDefault)) {
				valueRect.size.width = valueRect.size.width - 22;
			} 
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.enabled = NO;
			valueField.textAlignment = UITextAlignmentLeft;
			//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			if ([appItem objectForKey:@"style"]) {
				((MCTextField*)valueField.control).formulaStyle = [appItem objectForKey:@"style"];
			}
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.tag = valueTag;
			valueField.numberOfLines = 99;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			if (readFlag || bDefault) {
				cell.accessoryType = UITableViewCellAccessoryNone;
			} else {
				//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
			}
		}
			break;
	}
	
	if(cell.tag == -9999) {
		cell.selectionStyle = UITableViewCellAccessoryNone;
	} else {
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	}
}

+ (UITableViewCell *)createCellForEditForm:(UITableView *)tableView 
								   appItem:(NSMutableDictionary *)appItem
								 indexPath:(NSIndexPath *)indexPath
									   row:(int)row
								  formView:(UIView *)formView
										 x:(int)x
										 y:(int)y 
									target:(id)target{
	bool readFlag = [[appItem objectForKey:@"rdy"] boolValue];
	bool required = [[appItem objectForKey:@"rqd"] boolValue];
	//int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	//NSString *cellIdentifier = [NSString stringWithFormat:@"edit-cell-indexPath(section:%d,row:%d)", indexPath.section, row];
	NSString *cellIdentifier = [NSString stringWithFormat:@"%@%@%@-edit-cell-indexPath(section:%d,row:%d)-%@",fieldType,readFlag?@"T":@"F",required?@"T":@"F", indexPath.section, row, [MCUIToolkit getFieldName:appItem]];
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell) {
		return cell;
	}
	
	cell = [[[MCTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	BOOL nobr = NO;
	if (style && [style objectForKey:@"nobr"]) {
		nobr = [[style objectForKey:@"nobr"] boolValue];
	}
	if(nobr) {
		cell.tag = -9999;
	} else {
		cell.tag = row;
	}
	
	[self addCellForEditForm:tableView
					 appItem:appItem
				   indexPath:indexPath
						 row:row
					formView:formView
						cell:cell
						   x:x
						   y:y
					  target:target];
	
	return cell;
}


+ (void)selectDetailForEditFormByAppItem:(MCTableViewController*)viewController
								withCell:(UITableViewCell *)aCell 
								 appItem:(NSMutableDictionary *)appItem 
								  record:(NSMutableDictionary *)record 
							 isQueryForm:(bool)isQueryForm
									 tag:(int)tag {
	//char cReadFlag = [aCell.reuseIdentifier characterAtIndex:1];
	bool readFlag = [[appItem objectForKey:@"rdy"] boolValue];
	NSString *name;
	NSMutableDictionary *field;
	if (isQueryForm) {
		field = [appItem objectForKey:@"fld"];
		name = [field objectForKey:@"name"];
	}else {
		name = [MCUIToolkit getFieldName:appItem];
		field = [MCUIToolkit getField:appItem];
	}
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSMutableDictionary *fieldData = [[record objectForKey:@"data"] objectForKey:name];	
	
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
			break;
			
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		{
			if (readFlag) {//(cReadFlag == 'T')
				return;
			}
			NSString *lookupFieldType = @"1";
			if ([appItem objectForKey:@"lptype"]) {
				lookupFieldType = [appItem objectForKey:@"lptype"];
			}
			
			NSMutableArray *aQueryFilter = [appItem objectForKey:@"lpft"];
			if (aQueryFilter == nil) {
				aQueryFilter = [NSMutableArray array];
			}
			
			NSMutableArray *queryFilter = [MCUIToolkit getObjectCopy:aQueryFilter];
			
			for (int i = 0; i < [queryFilter count]; i++) {
				NSMutableDictionary *filter = [queryFilter objectAtIndex:i];
				if ([filter objectForKey:@"rfld"]) {
					NSMutableDictionary *relFieldData = [[record objectForKey:@"data"] objectForKey:[filter objectForKey:@"rfld"] == nil?[[NSMutableDictionary alloc]init]:[filter objectForKey:@"rfld"]];
					NSString *relValue = [relFieldData objectForKey:@"value"] == nil?@"":[relFieldData objectForKey:@"value"];
					[filter setObject:relValue forKey:@"value"];
					[filter removeObjectForKey:@"rfld"];
				}
			}
			NSString *dataObjectName = [MCUIToolkit getRelationObject:appItem];	
			
			if ([lookupFieldType isEqualToString:@"2"]) {
				NSString *lookupLayoutID = [appItem objectForKey:@"lplyt"]; 
				TreeLookupController *lookupController = [[TreeLookupController alloc] initWithStyle:UITableViewStylePlain];
				lookupController.fieldData = fieldData;
				lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
				lookupController.field = field;
				lookupController.layoutId = lookupLayoutID;
				lookupController.queryFilter = queryFilter;
				lookupController.datas = [record objectForKey:@"data"];
				lookupController.fieldName = name;
				
				[viewController.navigationController pushViewController:lookupController animated:YES];
				[lookupController getDataFromServer];
				
				[lookupController release];
			} else if ([lookupFieldType isEqualToString:@"3"]) {
				if (YES) {
					CustomLookupController *customLookupController = [[CustomLookupController alloc] init];
					customLookupController.layoutName = [appItem objectForKey:@"lplyt"];
					customLookupController.queryFilter = queryFilter;
					customLookupController.page = 1;
					customLookupController.fieldData = fieldData;
					customLookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
					customLookupController.field = field;
					customLookupController.datas = [record objectForKey:@"data"];
					customLookupController.fieldName = name;
					customLookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
					
					[viewController.navigationController pushViewController:customLookupController animated:YES];
					[customLookupController getDataFromServer];
					
					[customLookupController release];
				}
				else {
					CustomAListLookupController *customAListLookupController = [[CustomAListLookupController alloc] init];
					customAListLookupController.dataObjectName = @"";
					customAListLookupController.layoutName = [appItem objectForKey:@"lplyt"];
					customAListLookupController.queryFilter = queryFilter;
					customAListLookupController.page = 1;
					customAListLookupController.fieldData = fieldData;
					customAListLookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
					customAListLookupController.field = field;
					customAListLookupController.dataObjectName = dataObjectName;
					customAListLookupController.datas = [record objectForKey:@"data"];
					customAListLookupController.fieldName = name;
					customAListLookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];					
					[viewController.navigationController pushViewController:customAListLookupController animated:YES];
					[customAListLookupController getDataFromServer];
					
					[customAListLookupController release];
				}
			} else {
				id returnObject = [[MServerDAO sharedInstance] getListConfig:dataObjectName layoutName:[appItem objectForKey:@"lplyt"] == nil?@"":[appItem objectForKey:@"lplyt"]];
				if (returnObject) {
					
					NSMutableDictionary *listLayout = (NSMutableDictionary *)returnObject;
					if (cFieldType == '3' || cFieldType == '4') {
						if ([@"3" isEqualToString:[appItem objectForKey:@"lpst"]]) {
							MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
							LookupSelectView* lookupSelView = [LookupSelectView instance];
							lookupSelView.textFieldView = valueField;
							lookupSelView.fieldData = fieldData;
							lookupSelView.dataObjectName = dataObjectName;
							lookupSelView.listLayout = (NSMutableDictionary *)returnObject;
							lookupSelView.queryFilter = queryFilter;
							lookupSelView.datas = [record objectForKey:@"data"];
							lookupSelView.fieldName = name;
							lookupSelView.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
							lookupSelView.parentViewController = viewController;
							
							[lookupSelView getDataFromServer];
							return;
						}
					}
					
					if ([[listLayout objectForKey:@"style"] isEqualToString:@"3"]) {						
						AListLookupController *lookupController = [[AListLookupController alloc] initWithStyle:UITableViewStylePlain];
						lookupController.fieldData = fieldData;
						lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
						lookupController.field = field;
						lookupController.dataObjectName = dataObjectName;
						lookupController.listLayout = listLayout;
						lookupController.queryFilter = queryFilter;
						lookupController.page = 1;
						lookupController.datas = [record objectForKey:@"data"];
						lookupController.fieldName = name;
						lookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
						
						[viewController.navigationController pushViewController:lookupController animated:YES];
						[lookupController getDataFromServer];
						
						[lookupController release];
					}
					else {	
						LookupController *lookupController = [[LookupController alloc] init];
						lookupController.fieldData = fieldData;
						lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
						lookupController.field = field;
						lookupController.dataObjectName = dataObjectName;
						lookupController.listLayout = (NSMutableDictionary *)returnObject;
						lookupController.queryFilter = queryFilter;
						lookupController.page = 1;
						lookupController.datas = [record objectForKey:@"data"];
						lookupController.fieldName = name;
						lookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
						
						[viewController.navigationController pushViewController:lookupController animated:YES];
						[lookupController getDataFromServer];
						
						[lookupController release];
					}
				}
			}
		}
			break;
			
		case 'C': //日期
		{
			if (readFlag) {//(cReadFlag == 'T')
				return;
			}
			MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			if (valueField && valueField.tagEx > 0) {
				int section = valueField.tagEx / 100;
				int row = valueField.tagEx % 100;
				NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
				[viewController.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
			}
			
			DateTimeSelectView* dateSelView = [DateTimeSelectView instance];
			dateSelView.textFieldView = valueField;
			dateSelView.fieldData = fieldData;
			
			[[DateTimeSelectView instance] show:viewController withMode:UIDatePickerModeDate];
			
		}			
			break;
			
		case 'D': //日期时间
		{
			if (readFlag) {//(cReadFlag == 'T')
				return;
			}
			MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			if (valueField && valueField.tagEx > 0) {
				int section = valueField.tagEx / 100;
				int row = valueField.tagEx % 100;
				NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
				[viewController.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
			}
			DateTimeSelectView* dateSelView = [DateTimeSelectView instance];
			dateSelView.textFieldView = valueField;
			dateSelView.fieldData = fieldData;
			
			[[DateTimeSelectView instance] show:viewController withMode:UIDatePickerModeDateAndTime];
		}	
			break;
			
		case '8': //文本区
		case 'G': //文本区
		{	
			if (readFlag) {//(cReadFlag == 'T')
				int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
				if (lableStyle == 1 || lableStyle == 2) {
					return;
				}
			}
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			textFieldView.isReadOnly = readFlag;//(cReadFlag == 'T');
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];
		}
			break;
			
		case '0': //html
		{
			if (readFlag) {//(cReadFlag == 'T')
				NSString* htmlString = [fieldData objectForKey:@"value"];
				MCWebViewController* htmlView = [[MCWebViewController alloc] init];
				[viewController.navigationController pushViewController:htmlView animated:YES];
				[htmlView loadHTMLString:htmlString];
				[htmlView release];
				return;
			}
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.webView = (UIWebView *)[aCell.contentView viewWithTag:tag];
			textFieldView.isReadOnly = readFlag;//(cReadFlag == 'T');
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];
		}
			break;
			
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		{
			if (readFlag) {//(cReadFlag == 'T')
				return;
			}
			NSMutableDictionary *selectOptions = [record objectForKey:@"so"];
			NSMutableDictionary *option = [MCUIToolkit getselectOption:appItem
														  selectOption:selectOptions
															 fieldName:name];
			
			NSMutableArray *itemList = [[NSMutableArray alloc]init];
			if (option != nil && [option objectForKey:@"list"]) {
				itemList = [option objectForKey:@"list"];
			}
			
#if 1
			PopUpSelectFieldViewController *selectFieldView = [[PopUpSelectFieldViewController alloc] init];
			selectFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			selectFieldView.fieldData = fieldData;
			selectFieldView.field = field;
			selectFieldView.itemList = itemList;
			selectFieldView.datas = [record objectForKey:@"data"];
			selectFieldView.fieldName = name;
			
			//[viewController.navigationController pushViewController:selectFieldView animated:YES];
			[selectFieldView show:viewController.navigationController];
			[selectFieldView release];
#else
			SelectFieldViewController *selectFieldView = [[SelectFieldViewController alloc] init];
			selectFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			selectFieldView.fieldData = fieldData;
			selectFieldView.field = field;
			selectFieldView.itemList = itemList;
			selectFieldView.datas = [record objectForKey:@"data"];
			selectFieldView.fieldName = name;
			
			[viewController.navigationController pushViewController:selectFieldView animated:YES];
			[selectFieldView release];
#endif
		}
			break;
			
		case 'Y': //文件（多个）
		{
			FileListViewController *fileListView = [[FileListViewController alloc] initWithStyle:UITableViewStylePlain];
			fileListView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
			fileListView.fieldData = fieldData;
			fileListView.field = field;
			if (readFlag) {//(cReadFlag == 'T')
				fileListView.isViewMode = YES;
			}
			else {
				fileListView.isViewMode = NO;
			}
			[viewController.navigationController pushViewController:fileListView animated:YES];
			[fileListView release];
		}
			break;
			
		case 'M': //精确扫描
		case 'N': //简易扫描
		case 'P': //智能卡
		case 'R': //条形码
		case 'V': //gps
		case 'W': //彩码
		case 'X': //流媒体
			break;
		case 'T': //手机定位
		{
//			LocationView *locationView = [[LocationView alloc] init];
//			locationView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];
//			
//			locationView.fieldData = fieldData;
//			locationView.field = field;
//			if (readFlag) {//(cReadFlag == 'T')
//				locationView.isViewMode = YES;
//			}else {
//				locationView.isViewMode = NO;
//			}
//			
//			[viewController.navigationController pushViewController:locationView animated:YES];
//			[locationView release];
		}
			break;
			/*
			 case '0': //html
			 {
			 NSString* htmlString = [fieldData objectForKey:@"value"];
			 MCWebViewController* htmlView = [[MCWebViewController alloc] init];
			 [viewController.navigationController pushViewController:htmlView animated:YES];
			 [htmlView loadHTMLString:htmlString];
			 [htmlView release];
			 }
			 break;*/
		default:
		{
			return;
		}
			break;
	}
	
}

#pragma mark --- 高级样式，一行多列 浏览表单开始 ---

+ (void)addCellForViewForm:(UITableView *)tableView 
				   appItem:(NSMutableDictionary *)appItem
				 indexPath:(NSIndexPath *)indexPath
					   row:(int)row
				  formView:(UIView *)formView
					  cell:(UITableViewCell *)cell 
						 x:(int)x
						 y:(int)y 
					target:(id)target {
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	int labelTag = -99 + row;
	int valueTag = -9999 + indexPath.section*100 + row;// -2
	UIView *uiview = [cell.contentView viewWithTag:labelTag];
	if(uiview) {
		return;
	}
	
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	int width = 320 - nFormLabelLeftSpace*2, lblwidth = nFormLabelWidth, valuewidth = width;
	if (style != nil) {
		if ([style objectForKey:@"width"]) {
			width = [[style objectForKey:@"width"] intValue] * 320 / 100 - nFormLabelLeftSpace*2;// 百分比
		}
		
		if ([style objectForKey:@"lblwidth"]) {
			lblwidth = [[style objectForKey:@"lblwidth"] intValue] * 320 / 100;// 百分比
		} 
	}
	
	if(lableStyle == 1) {
		lblwidth = width;
	} else if(lableStyle == 2) {
		lblwidth = 0;
	}
	
	if(lableStyle == 0) {
		valuewidth = width - lblwidth;
	} else {
		valuewidth = width;
	}
	
	UILabel *labelField = [[UILabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(x + 10, y + 4, lableStyle==1?(lblwidth - 5):(lblwidth - 10), 35)];
	//UILabel *labelField = [[UILabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(10, 4.0, lableStyle==1?300:95, 35)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:16];
	labelField.autoresizingMask = nTableCellAutoresizingMask;
	if (style != nil) {
		float fontSize = 18.0;
		if ([style objectForKey:@"label_color"]) {
			labelField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"label_color"]]];
		}
		if ([style objectForKey:@"label_size"]) {
			fontSize = [[style objectForKey:@"label_size"] floatValue]/0.75;
			labelField.font = [UIFont boldSystemFontOfSize:fontSize];
		}
		if ([style objectForKey:@"label_style"]) {
			NSString *fontStyle = [style objectForKey:@"label_style"];
			if ([fontStyle isEqualToString:@"B"]) {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}else if ([fontStyle isEqualToString:@"I"]) {
				labelField.font = [UIFont italicSystemFontOfSize:fontSize];
			}else {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
		}
	}
	labelField.numberOfLines = 1;
	labelField.tag = labelTag;
	[cell.contentView addSubview:labelField];
	[labelField release];
	
	BOOL canSelect = NO;
	if(cell.tag != -9999) {
		canSelect = YES;
	}
	
	CGRect valueRect = CGRectMake(lableStyle>0?(x + 10):(x + lblwidth + 10), lableStyle==1?(y + 40): (y + 10), valuewidth, 44);
	//CGRect valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 9999);
	if (cFieldType == 'O') {
		MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
		valueField.textAlignment = UITextAlignmentLeft;
		//		valueField.textColor = [UIColor grayColor];
		valueField.backgroundColor = [UIColor clearColor];
		valueField.font = [UIFont boldSystemFontOfSize:16];
		valueField.fieldType = cFieldType;
		valueField.tag = valueTag;
		valueField.autoresizingMask = nTableCellAutoresizingMask;
		valueField.secureTextEntry = YES;
		valueField.enabled = NO;
		[cell.contentView addSubview:valueField];
		[valueField release];
	} else if (cFieldType == 'S') {
		if(canSelect) {
			valueRect.size.width = valueRect.size.width - 22;
		}
		
		//valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 35);
		MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
		//imgView.autoresizingMask = nTableCellAutoresizingMask;
		imgView.tag = valueTag;
		[cell.contentView addSubview:imgView];
		[imgView release];
	} else if (cFieldType == '0') {
		valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
		MCWebView* webView = [[MCWebView alloc] initWithFrame:valueRect];
		[webView setBackgroundColor:[UIColor clearColor]];
		webView.scalesPageToFit = NO;
		webView.autoresizingMask = nTableCellAutoresizingMask;
		webView.tag = valueTag;
		webView.userInteractionEnabled = YES;
		webView.section = indexPath.section;
		webView.row = row;//indexPath.row
		[cell.contentView addSubview:webView];
		[webView release];
	} else {	
		MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
		valueField.textAlignment = UITextAlignmentLeft;
		//		valueField.textColor = [UIColor grayColor];
		valueField.backgroundColor = [UIColor clearColor];
		valueField.font = [UIFont boldSystemFontOfSize:16];
		valueField.numberOfLines = 99;
		valueField.fieldType = cFieldType;
		valueField.tag = valueTag;
		valueField.autoresizingMask = nTableCellAutoresizingMask;
		[cell.contentView addSubview:valueField];
		[valueField release];
	}
	switch (cFieldType) {
		case 'S': //图片
		case 'J': //文件
		case 'T': //手机定位	
		case 'Y': //文件（多个）
			//case 'F': //文本
		case '9': //电话
		case 'A': //电子邮件
		case 'Q': //手机号码
		case '5': //超级链接
		case '8': //文本区
		case 'G': //文本区
		case '0': //html
			if(!canSelect) {
				cell.accessoryType = UITableViewCellAccessoryNone;
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
			} 
			if (cFieldType == '8' || cFieldType == 'G' || cFieldType == '0') {
				cell.accessoryType = UITableViewCellAccessoryNone;
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
			} else {
				//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				if(cFieldType != '5') {
					[self didSelectRowIndexPath:cell x:x y:y width:width height:44 tag:valueTag target:target];
				}
			}
			break;
		default:
			cell.accessoryType = UITableViewCellAccessoryNone;
			break;
	}
	
	if(cell.tag == -9999) {
		cell.selectionStyle = UITableViewCellAccessoryNone;
	} else {
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	}
}

+ (UITableViewCell *)createCellForViewForm:(UITableView *)tableView 
								   appItem:(NSMutableDictionary *)appItem
								 indexPath:(NSIndexPath *)indexPath
									   row:(int)row
								  formView:(UIView *)formView
										 x:(int)x
										 y:(int)y 
									target:(id)target {
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSString *cellIdentifier = [NSString stringWithFormat:@"%@-view-cell-section=%d-row=%d-%@",fieldType,
								indexPath.section, row, [MCUIToolkit getFieldName:appItem]];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell) {
		return cell;
	}
	
	//CGRect cellFrame = CGRectMake(0, 0, 320, 40);
	cell = [[[MCTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
	
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	BOOL nobr = NO;
	if (style && [style objectForKey:@"nobr"]) {
		nobr = [[style objectForKey:@"nobr"] boolValue];
	}
	if(nobr) {
		cell.tag = -9999;
	} else {
		cell.tag = row;
	}
	
	[self addCellForViewForm:tableView
					 appItem:appItem
				   indexPath:indexPath
						 row:row
					formView:formView
						cell:cell
						   x:x
						   y:y
					  target:target];
	
	return cell;
}

+ (void)selectDetailForViewForm:(UIViewController*)viewController
					   withCell:(UITableViewCell *)aCell 
						appItem:(NSMutableDictionary *)appItem 
						   data:(NSMutableDictionary *)fieldData
							tag:(int)tag {
	
	NSMutableDictionary *field = [MCUIToolkit getField:appItem];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '3': //主－详细关系
		case '4': //查找关系
		case '6': //百分比
		case '7': //金额
		case 'E': //数字
		case 'B': //复选框
		case 'C': //日期
		case 'D': //日期时间
			break;
			
		case '5': //超级链接
		{
			if (nil != [fieldData objectForKey:@"value"] && [[fieldData objectForKey:@"value"] length] > 0) {
				[MCFunction goUrl:viewController withUrl:[fieldData objectForKey:@"value"]];
			}
		}
			break;
		case '9': //电话
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
				fieldAction.actionUrls = [[NSMutableArray alloc] 
										  initWithObjects:
										  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
										  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
				[actionSheet addButtonWithTitle:@"拨打电话"];
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话!"];
			}
		}
			break;
		case 'A': //电子邮件
		{
			FieldAction *fieldAction = [[FieldAction alloc] init];
			fieldAction.viewController = viewController;
			fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
									  [NSString stringWithFormat:@"mailto:%@",[fieldData objectForKey:@"value"]],
									  nil];
			UIActionSheet *actionSheet = [[UIActionSheet alloc]
										  initWithTitle:@"请选择操作"
										  delegate:fieldAction
										  cancelButtonTitle:nil
										  destructiveButtonTitle:nil 
										  otherButtonTitles:nil];
			[actionSheet addButtonWithTitle:@"发送邮件"];
			int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
			actionSheet.cancelButtonIndex = cancelIndex;
			[actionSheet showInView:viewController.clientView.window];
			[actionSheet release];
			
		}
			break;
		case 'Q': //手机号码
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
				fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
										  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
										  [NSString stringWithFormat:@"sms://%@",[fieldData objectForKey:@"value"]],
										  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
				[actionSheet addButtonWithTitle:@"拨打电话"];
				[actionSheet addButtonWithTitle:@"发送短信"];
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话和发短信!"];
			}
		}
			break;
			
		case 'F': //文本
		case '8': //文本区
		case 'G': //文本区
		{
			return;
			/*
			 int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
			 if (lableStyle == 1 || lableStyle == 2) {
			 return;
			 }
			 TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			 textFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			 
			 textFieldView.isReadOnly = YES;
			 textFieldView.fieldData = fieldData;
			 textFieldView.field = field;
			 [viewController.navigationController pushViewController:textFieldView animated:YES];
			 [textFieldView release];*/
		}
			break;
			
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'O': //密码
			break;
			
		case 'S': //图片
		case 'J': //文件
		{
			NSString *dataFrom = [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
			NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
			if ([fileInfos count] != 1) {
				return;
			}
			NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
			
			[self filePreview:viewController
					 dataFrom:dataFrom
					 fileInfo:fileInfo];
			
			/*
			 NSString *fileName = [fileInfo objectForKey:@"fname"];
			 if (fileName == nil || [fileName length] == 0)
			 return;
			 if ([fileName hasSuffix:@"avi"]) {
			 [[MCErrorManagement instance] doError:@"99" returnMessage:@"视频文件暂不支持播放功能!"];
			 return;
			 }
			 
			 NSString* szExtension = [[fileName pathExtension] lowercaseString];
			 if ([szExtension isEqualToString:@"rar"] || [szExtension isEqualToString:@"zip"]) {
			 RarOrZipController *rarOrZipView = [[RarOrZipController alloc] initWithStyle:UITableViewStylePlain];
			 [viewController.navigationController pushViewController:rarOrZipView animated:YES];
			 rarOrZipView.title = fileName;
			 
			 NSMutableDictionary *fileParam = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
			 fileName,@"fname",
			 [fileInfo objectForKey:@"lcn"],@"lcn",nil];
			 
			 [rarOrZipView getDataFromServer:dataFrom
			 fileParam:fileParam];
			 [fileParam release];
			 [rarOrZipView release];
			 return;
			 }
			 
			 
			 NSString *actionType = @"download";
			 if ([szExtension isEqualToString:@"doc"] || [szExtension isEqualToString:@"txt"]) {
			 actionType = @"preview";
			 } 
			 
			 NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
			 NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
			 fileName,@"fileName",
			 fileLocation,@"fileLocation",
			 actionType,@"actionType",
			 dataFrom,@"dataFrom",nil];
			 FileViewController *fileView = [[FileViewController alloc] init];
			 [viewController.navigationController pushViewController:fileView animated:YES];
			 fileView.title = @"文件下载中...";
			 fileView.fileAttribute = fileAttribute;
			 [fileAttribute release];
			 [fileView getDataFromServer];
			 [fileView release];
			 */
			return;
		}
			break;
		case '0': //html
		{
			NSString* htmlString = [fieldData objectForKey:@"value"];
			MCWebViewController* htmlView = [[MCWebViewController alloc] init];
			[viewController.navigationController pushViewController:htmlView animated:YES];
			[htmlView loadHTMLString:htmlString];
			[htmlView release];
		}
			break;
		case 'T': //手机定位
		{
//			LocationView *locationView = [[LocationView alloc] init];
//			locationView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];//-2
//			
//			locationView.fieldData = fieldData;
//			locationView.field = field;
//			locationView.isViewMode = YES;
//			
//			[viewController.navigationController pushViewController:locationView animated:YES];
//			[locationView release];
		}
			break;
			
		case 'Y': //文件（多个）
		{
			if (nil == [fieldData objectForKey:@"fi"] || 0 == [[fieldData objectForKey:@"fi"] count]) {
				return;
			}
			FileListViewController *fileListView = [[FileListViewController alloc] initWithStyle:UITableViewStylePlain];
			fileListView.valueField = (MCControl *)[aCell.contentView viewWithTag:tag];//-2
			
			fileListView.fieldData = fieldData;
			fileListView.field = field;
			fileListView.isViewMode = YES;
			
			[viewController.navigationController pushViewController:fileListView animated:YES];
			[fileListView release];
		}
			break;
			
		case 'M': //精确扫描
		case 'N': //简易扫描
		case 'P': //智能卡
		case 'R': //条形码
		case 'U': //多选过滤
		case 'V': //gps
		case 'W': //彩码
		case 'X': //流媒体
			break;
		default:
			break;
	}
}

#pragma mark --- 高级样式，一行多列 结束 ---


+ (UITableViewCell *)createCellForEditForm:(UITableView *)tableView 
								   appItem:(NSMutableDictionary *)appItem
								 indexPath:(NSIndexPath *)indexPath
								  formView:(UIView *)formView
{
	bool readFlag = [[appItem objectForKey:@"rdy"] boolValue];
	bool required = [[appItem objectForKey:@"rqd"] boolValue];
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSString *cellIdentifier = [NSString stringWithFormat:@"%@%@%@-edit-cell-indexPath(section:%d,row:%d)-%@",fieldType,readFlag?@"T":@"F",required?@"T":@"F", indexPath.section, indexPath.row, [MCUIToolkit getFieldName:appItem]];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell) {
		return cell;
	}
	
	// CGRect cellFrame = CGRectMake(0, 0, 320, 120);
	cell = [[[MCTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	UIView* titleBackView = [[UIView alloc] initWithFrame:lableStyle==2? CGRectZero:CGRectMake(0, 0, lableStyle==1?305:100, 44)];
	titleBackView.backgroundColor = [MCUserDefaults instance].viewCellTitleBackgroundColor;
	[cell.contentView addSubview:titleBackView];
	[titleBackView release];
	
	MCLabel *labelField = [[MCLabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(10, 0, lableStyle==1?295:95, 44)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:16];
	
	if (required) {
		[self labelColor:labelField];
		labelField.toastMessage = @"此为必填项！";
	}
	[titleBackView addSubview:labelField];
	[labelField release];
	
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	if (style != nil) {
		float fontSize = 18.0;
		if ([style objectForKey:@"label_color"]) {
			labelField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"label_color"]]];
		}
		if ([style objectForKey:@"label_size"]) {
			fontSize = [[style objectForKey:@"label_size"] floatValue]/0.75;
			labelField.font = [UIFont boldSystemFontOfSize:fontSize];
		}
		if ([style objectForKey:@"label_style"]) {
			NSString *fontStyle = [style objectForKey:@"label_style"];
			if ([fontStyle isEqualToString:@"B"]) {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}else if ([fontStyle isEqualToString:@"I"]) {
				labelField.font = [UIFont italicSystemFontOfSize:fontSize];
			}else {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
		}
	}
	labelField.numberOfLines = 1;
	labelField.tag = -1;
	
	/*
	UILabel *spaceField = [[UILabel alloc] initWithFrame:CGRectMake(lableStyle>0?8:108, lableStyle==1?38:10, 2, 22)];
	spaceField.backgroundColor = [UIColor clearColor];
	[cell.contentView addSubview:spaceField];
	[spaceField release];
	if (required) {
		spaceField.backgroundColor = [UIColor redColor];
	}
	*/

	CGRect valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:190, 999);
	BOOL bDefault = YES;

	switch (cFieldType) {
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			
			if ([appItem objectForKey:@"style"]) {
				((MCTextField*)valueField.control).formulaStyle = [appItem objectForKey:@"style"];
			}
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
				labelField.input = (MCTextField*)valueField.control;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
			if (cFieldType == 'O') {
				valueField.secureTextEntry = YES;
			} else if (cFieldType == 'A') {
				valueField.keyboardType = UIKeyboardTypeEmailAddress;
			} else if (cFieldType == '5') {
				valueField.keyboardType = UIKeyboardTypeURL;
			} else if (cFieldType == '6' || cFieldType == '7' || cFieldType == 'E') {
				valueField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
			} else if (cFieldType == '9' || cFieldType == 'Q') {
				valueField.keyboardType = UIKeyboardTypePhonePad;
			} else {
				valueField.keyboardType = UIKeyboardTypeDefault;
			}
		}
		break;
		case 'N': //验证码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.frame = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?200:100, 999);
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.keyboardType = UIKeyboardTypeDefault;
			valueField.tag = -2;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
				labelField.input = (MCTextField*)valueField.control;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			UIWebView *checkImage = [[UIWebView alloc] initWithFrame:CGRectMake(240, lableStyle==1?40:12, 60, 20)];
			checkImage.tag = -3;
			[cell.contentView addSubview:checkImage];
			[checkImage release];
		}
		break;
		case 'B': //复选框
		{
			MCSwitch *valueField = [[MCSwitch alloc] initWithFrame:valueRect];
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.tag = -2;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
		}
		break;
		case 'Y': //文件（多个）
		case '8': //文本区
		case 'G': //文本区
		case 'T': //手机定位
		case 'J': //文件
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.numberOfLines = 99;
			valueField.tag = -2;
			[cell.contentView addSubview:valueField];
			[valueField release];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			if (cFieldType == '8' || cFieldType == 'G') {
				if (readFlag) {
					if (lableStyle == 1 || lableStyle == 2) {
						cell.accessoryType = UITableViewCellAccessoryNone;
						cell.selectionStyle = UITableViewCellSelectionStyleNone;
					}
				}
			}
		}
		break;
		case '0': //html
		{	
			valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
			UIWebView* webView = [[UIWebView alloc] initWithFrame:valueRect];
			webView.scalesPageToFit = NO;
			webView.tag = -2;
			webView.userInteractionEnabled = NO;
			[cell.contentView addSubview:webView];
			[webView release];
		}
		break;
		case 'S': //图片
		{
			if (readFlag) {
				valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 35);
				MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
				imgView.tag = -2;
				[cell.contentView addSubview:imgView];
				[imgView release];
			} else {
				MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
				valueField.textAlignment = UITextAlignmentLeft;
//				valueField.textColor = [UIColor grayColor];
				valueField.backgroundColor = [UIColor clearColor];
				valueField.font = [UIFont boldSystemFontOfSize:16];
				valueField.tagEx = indexPath.section*100 +indexPath.row;
				valueField.fieldType = cFieldType;
				valueField.autoresizingMask = nTableCellAutoresizingMask;
				valueField.numberOfLines = 99;
				valueField.tag = -2;
				[cell.contentView addSubview:valueField];
				[valueField release];
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			}
		}
		break;
				
		/*
		case '0': //html
		{
			valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
			UIWebView* webView = [[UIWebView alloc] initWithFrame:valueRect];
			webView.scalesPageToFit = NO;
			webView.tag = -2;
			webView.userInteractionEnabled = NO;
			[cell.contentView addSubview:webView];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
		break;
		*/
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'C': //日期
		case 'D': //日期时间
			bDefault = NO;
		case '1': //公式
		case '2': //自动编号
		default:
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.enabled = NO;
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			if ([appItem objectForKey:@"style"]) {
				((MCTextField*)valueField.control).formulaStyle = [appItem objectForKey:@"style"];
			}
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.tag = -2;
			valueField.numberOfLines = 99;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			if (readFlag || bDefault) 
				cell.accessoryType = UITableViewCellAccessoryNone;
			else
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
		break;
	}
	
	return cell;
}

+ (void)selectDetailForEditFormByAppItem:(MCTableViewController*)viewController
								withCell:(UITableViewCell *)aCell 
								 appItem:(NSMutableDictionary *)appItem 
								  record:(NSMutableDictionary *)record 
							 isQueryForm:(bool)isQueryForm {
	char cReadFlag = [aCell.reuseIdentifier characterAtIndex:1];
	NSString *name;
	NSMutableDictionary *field;
	if (isQueryForm) {
		field = [appItem objectForKey:@"fld"];
		name = [field objectForKey:@"name"];
	}else {
		name = [MCUIToolkit getFieldName:appItem];
		field = [MCUIToolkit getField:appItem];
	}
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSMutableDictionary *fieldData = [[record objectForKey:@"data"] objectForKey:name];	
	
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
			break;
			
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		{
			if (cReadFlag == 'T') {
				return;
			}
			NSString *lookupFieldType = @"1";
			if ([appItem objectForKey:@"lptype"]) {
				lookupFieldType = [appItem objectForKey:@"lptype"];
			}
			
			NSMutableArray *aQueryFilter = [appItem objectForKey:@"lpft"];
			if (aQueryFilter == nil) {
				aQueryFilter = [NSMutableArray array];
			}
			
			NSMutableArray *queryFilter = [MCUIToolkit getObjectCopy:aQueryFilter];
			
			for (int i = 0; i < [queryFilter count]; i++) {
				NSMutableDictionary *filter = [queryFilter objectAtIndex:i];
				if ([filter objectForKey:@"rfld"]) {
					NSMutableDictionary *relFieldData = [[record objectForKey:@"data"] objectForKey:[filter objectForKey:@"rfld"] == nil?[[NSMutableDictionary alloc]init]:[filter objectForKey:@"rfld"]];
					NSString *relValue = [relFieldData objectForKey:@"value"] == nil?@"":[relFieldData objectForKey:@"value"];
					[filter setObject:relValue forKey:@"value"];
					[filter removeObjectForKey:@"rfld"];
				}
			}
			NSString *dataObjectName = [MCUIToolkit getRelationObject:appItem];	
			
			if ([lookupFieldType isEqualToString:@"2"]) {
				NSString *lookupLayoutID = [appItem objectForKey:@"lplyt"]; 
				TreeLookupController *lookupController = [[TreeLookupController alloc] initWithStyle:UITableViewStylePlain];
				lookupController.fieldData = fieldData;
				lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
				lookupController.field = field;
				lookupController.layoutId = lookupLayoutID;
				lookupController.queryFilter = queryFilter;
				lookupController.datas = [record objectForKey:@"data"];
				lookupController.fieldName = name;
	
				[viewController.navigationController pushViewController:lookupController animated:YES];
				[lookupController getDataFromServer];
				
				[lookupController release];
			} else if ([lookupFieldType isEqualToString:@"3"]) {
				if (YES) {
					CustomLookupController *customLookupController = [[CustomLookupController alloc] init];
					customLookupController.layoutName = [appItem objectForKey:@"lplyt"];
					customLookupController.queryFilter = queryFilter;
					customLookupController.page = 1;
					customLookupController.fieldData = fieldData;
					customLookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
					customLookupController.field = field;
					customLookupController.datas = [record objectForKey:@"data"];
					customLookupController.fieldName = name;
					customLookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
					
					[viewController.navigationController pushViewController:customLookupController animated:YES];
					[customLookupController getDataFromServer];
					
					[customLookupController release];
				}
				else {
					CustomAListLookupController *customAListLookupController = [[CustomAListLookupController alloc] init];
					customAListLookupController.dataObjectName = @"";
					customAListLookupController.layoutName = [appItem objectForKey:@"lplyt"];
					customAListLookupController.queryFilter = queryFilter;
					customAListLookupController.page = 1;
					customAListLookupController.fieldData = fieldData;
					customAListLookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
					customAListLookupController.field = field;
					customAListLookupController.dataObjectName = dataObjectName;
					customAListLookupController.datas = [record objectForKey:@"data"];
					customAListLookupController.fieldName = name;
					customAListLookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];					
					[viewController.navigationController pushViewController:customAListLookupController animated:YES];
					[customAListLookupController getDataFromServer];
					
					[customAListLookupController release];
				}
				
			} else {
				id returnObject = [[MServerDAO sharedInstance] getListConfig:dataObjectName layoutName:[appItem objectForKey:@"lplyt"] == nil?@"":[appItem objectForKey:@"lplyt"]];
				if (returnObject) {
				
					NSMutableDictionary *listLayout = (NSMutableDictionary *)returnObject;
					if (cFieldType == '3' || cFieldType == '4') {
						if ([@"3" isEqualToString:[appItem objectForKey:@"lpst"]]) {
							MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
							LookupSelectView* lookupSelView = [LookupSelectView instance];
							lookupSelView.textFieldView = valueField;
							lookupSelView.fieldData = fieldData;
							lookupSelView.dataObjectName = dataObjectName;
							lookupSelView.listLayout = (NSMutableDictionary *)returnObject;
							lookupSelView.queryFilter = queryFilter;
							lookupSelView.datas = [record objectForKey:@"data"];
							lookupSelView.fieldName = name;
							lookupSelView.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
							lookupSelView.parentViewController = viewController;
							
							[lookupSelView getDataFromServer];
							return;
						}
					}
					
					if ([[listLayout objectForKey:@"style"] isEqualToString:@"3"]) {						
						AListLookupController *lookupController = [[AListLookupController alloc] initWithStyle:UITableViewStylePlain];
						lookupController.fieldData = fieldData;
						lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
						lookupController.field = field;
						lookupController.dataObjectName = dataObjectName;
						lookupController.listLayout = listLayout;
						lookupController.queryFilter = queryFilter;
						lookupController.page = 1;
						lookupController.datas = [record objectForKey:@"data"];
						lookupController.fieldName = name;
						lookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
						
						[viewController.navigationController pushViewController:lookupController animated:YES];
						[lookupController getDataFromServer];
						
						[lookupController release];
					}
					else {	
						LookupController *lookupController = [[LookupController alloc] init];
						lookupController.fieldData = fieldData;
						lookupController.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
						lookupController.field = field;
						lookupController.dataObjectName = dataObjectName;
						lookupController.listLayout = (NSMutableDictionary *)returnObject;
						lookupController.queryFilter = queryFilter;
						lookupController.page = 1;
						lookupController.datas = [record objectForKey:@"data"];
						lookupController.fieldName = name;
						lookupController.lookupBackFills = [appItem objectForKey:@"lpbf"] == nil?[NSMutableArray array]:[appItem objectForKey:@"lpbf"];
					
						[viewController.navigationController pushViewController:lookupController animated:YES];
						[lookupController getDataFromServer];
					
						[lookupController release];
					}
				}
			}
		}
			break;
			
			
		case 'C': //日期
		{
			if (cReadFlag == 'T') {
				return;
			}
			MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			if (valueField && valueField.tagEx > 0) {
				int section = valueField.tagEx / 100;
				int row = valueField.tagEx % 100;
				NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
				[viewController.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
			}
			
			DateTimeSelectView* dateSelView = [DateTimeSelectView instance];
			dateSelView.textFieldView = valueField;
			dateSelView.fieldData = fieldData;
			
			[[DateTimeSelectView instance] show:viewController withMode:UIDatePickerModeDate];
			
		}			
			break;
			
		case 'D': //日期时间
		{
			if (cReadFlag == 'T') {
				return;
			}
			MCControl *valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			if (valueField && valueField.tagEx > 0) {
				int section = valueField.tagEx / 100;
				int row = valueField.tagEx % 100;
				NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
				[viewController.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
			}
			DateTimeSelectView* dateSelView = [DateTimeSelectView instance];
			dateSelView.textFieldView = valueField;
			dateSelView.fieldData = fieldData;
			
			[[DateTimeSelectView instance] show:viewController withMode:UIDatePickerModeDateAndTime];
		}	
			break;
			
		case '8': //文本区
		case 'G': //文本区
		{	
			if (cReadFlag == 'T') {
				int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
				if (lableStyle == 1 || lableStyle == 2) {
					return;
				}
			}
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			textFieldView.isReadOnly = (cReadFlag == 'T');
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];
		}
			break;
			
		case '0': //html
		{
			if (cReadFlag == 'T') {
				NSString* htmlString = [fieldData objectForKey:@"value"];
				MCWebViewController* htmlView = [[MCWebViewController alloc] init];
				[viewController.navigationController pushViewController:htmlView animated:YES];
				[htmlView loadHTMLString:htmlString];
				[htmlView release];
				return;
			}
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.webView = (UIWebView *)[aCell.contentView viewWithTag:-2];
			textFieldView.isReadOnly = (cReadFlag == 'T');
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];
		}
			break;
			
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		{
			if (cReadFlag == 'T') {
				return;
			}
			NSMutableDictionary *selectOptions = [record objectForKey:@"so"];
			NSMutableDictionary *option = [MCUIToolkit getselectOption:appItem
														  selectOption:selectOptions
															 fieldName:name];
			
			NSMutableArray *itemList = [[NSMutableArray alloc]init];
			if (option != nil && [option objectForKey:@"list"]) {
				itemList = [option objectForKey:@"list"];
			}
			
		#if 1
			PopUpSelectFieldViewController *selectFieldView = [[PopUpSelectFieldViewController alloc] init];
			selectFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			selectFieldView.fieldData = fieldData;
			selectFieldView.field = field;
			selectFieldView.itemList = itemList;
			selectFieldView.datas = [record objectForKey:@"data"];
			selectFieldView.fieldName = name;
			
			//[viewController.navigationController pushViewController:selectFieldView animated:YES];
			[selectFieldView show:viewController.navigationController];
			[selectFieldView release];
		#else
			SelectFieldViewController *selectFieldView = [[SelectFieldViewController alloc] init];
			selectFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			selectFieldView.fieldData = fieldData;
			selectFieldView.field = field;
			selectFieldView.itemList = itemList;
			selectFieldView.datas = [record objectForKey:@"data"];
			selectFieldView.fieldName = name;
			
			[viewController.navigationController pushViewController:selectFieldView animated:YES];
			[selectFieldView release];
		#endif
		}
			break;
			
		case 'Y': //文件（多个）
		{
			FileListViewController *fileListView = [[FileListViewController alloc] initWithStyle:UITableViewStylePlain];
			fileListView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			fileListView.fieldData = fieldData;
			fileListView.field = field;
			if (cReadFlag == 'T') {
				fileListView.isViewMode = YES;
			}
			else {
				fileListView.isViewMode = NO;
			}
			[viewController.navigationController pushViewController:fileListView animated:YES];
			[fileListView release];
		}
			break;
			
		case 'M': //精确扫描
		case 'N': //简易扫描
		case 'P': //智能卡
		case 'R': //条形码
		case 'V': //gps
		case 'W': //彩码
		case 'X': //流媒体
			break;
		case 'T': //手机定位
		{
//			LocationView *locationView = [[LocationView alloc] init];
//			locationView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
//			
//			locationView.fieldData = fieldData;
//			locationView.field = field;
//			if (cReadFlag == 'T') {
//				locationView.isViewMode = YES;
//			}else {
//				locationView.isViewMode = NO;
//			}
//
//			[viewController.navigationController pushViewController:locationView animated:YES];
//			[locationView release];
		}
		break;
		/*
		case '0': //html
		{
			NSString* htmlString = [fieldData objectForKey:@"value"];
			MCWebViewController* htmlView = [[MCWebViewController alloc] init];
			[viewController.navigationController pushViewController:htmlView animated:YES];
			[htmlView loadHTMLString:htmlString];
			[htmlView release];
		}
		break;*/
		default:
		{
			return;
		}
			break;
	}
	
}

+ (UITableViewCell *)createCellForCustomForm:(UITableView *)tableView 
									 appItem:(NSMutableDictionary *)appItem
								   indexPath:(NSIndexPath *)indexPath
									formView:(UIView *)formView {
	bool readFlag = [[appItem objectForKey:@"rdy"] boolValue];
	bool required = [[appItem objectForKey:@"rqd"] boolValue];
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSString *cellIdentifier = [NSString stringWithFormat:@"%@%@%@-edit-cell-indexPath(section:%d,row:%d)-%@",fieldType,readFlag?@"T":@"F",required?@"T":@"F",
								indexPath.section, indexPath.row, [MCUIToolkit getFieldName:appItem]];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell) {
		return cell;
	}
	
	//CGRect cellFrame = CGRectMake(0, 0, 500, 40);
	cell = [[[MCTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
	cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	//UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 43)];
	//bg.backgroundColor = [UIColor whiteColor];
	//[cell addSubview:bg];
	//[cell sendSubviewToBack:bg];
	//[bg release];
	
	UILabel *labelField = [[UILabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(10, 4.0, lableStyle==1?300:95, 35)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:16];
	if (required) {
		[self labelColor:labelField];
	}
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	if (style != nil) {
		float fontSize = 18.0;
		if ([style objectForKey:@"label_color"]) {
			labelField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"label_color"]]];
		}
		if ([style objectForKey:@"label_size"]) {
			fontSize = [[style objectForKey:@"label_size"] floatValue]/0.75;
			labelField.font = [UIFont boldSystemFontOfSize:fontSize];
		}
		if ([style objectForKey:@"label_style"]) {
			NSString *fontStyle = [style objectForKey:@"label_style"];
			if ([fontStyle isEqualToString:@"B"]) {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}else if ([fontStyle isEqualToString:@"I"]) {
				labelField.font = [UIFont italicSystemFontOfSize:fontSize];
			}else {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
		}
	}
	labelField.numberOfLines = 1;
	labelField.tag = -1;
	[cell.contentView addSubview:labelField];
	[labelField release];
	
	CGRect valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:190, 999);
	BOOL bDefault = YES;
	switch (cFieldType) {
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
			if (cFieldType == 'O') {
				valueField.secureTextEntry = YES;
			} else if (cFieldType == 'A') {
				valueField.keyboardType = UIKeyboardTypeEmailAddress;
			} else if (cFieldType == '5') {
				valueField.keyboardType = UIKeyboardTypeURL;
			} else if (cFieldType == '6' || cFieldType == '7' || cFieldType == 'E') {
				valueField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
			} else if (cFieldType == '9' || cFieldType == 'Q') {
				valueField.keyboardType = UIKeyboardTypePhonePad;
			} else {
				valueField.keyboardType = UIKeyboardTypeDefault;
			}
		}
		break;
		case 'B': //复选框
		{
			MCSwitch *valueField = [[MCSwitch alloc] initWithFrame:valueRect];
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.tag = -2;
			if (readFlag) {
				valueField.enabled = NO;
			}else {
				valueField.enabled = YES;
			}
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
		}
		break;
		case 'Y': //文件（多个）
		case '8': //文本区
		case 'G': //文本区
		case 'T': //手机定位
		case 'J': //文件
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.numberOfLines = 99;
			[cell.contentView addSubview:valueField];
			[valueField release];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			if (cFieldType == '8' || cFieldType == 'G') {
				if (readFlag) {
					if (lableStyle == 1 || lableStyle == 2) {
						cell.accessoryType = UITableViewCellAccessoryNone;
						cell.selectionStyle = UITableViewCellSelectionStyleNone;
					}
				}
			}
		}
		break;
		case 'S': //图片
		{
			if (readFlag) {
				valueRect = CGRectMake(lableStyle>0?10:80, lableStyle==1?40:10, lableStyle>0?300:230, 35);
				MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
				imgView.tag = -2;
				[cell.contentView addSubview:imgView];
				[imgView release];
			} else {
				MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
				valueField.textAlignment = UITextAlignmentLeft;
//				valueField.textColor = [UIColor grayColor];
				valueField.backgroundColor = [UIColor clearColor];
				valueField.font = [UIFont boldSystemFontOfSize:16];
				valueField.tagEx = indexPath.section*100 +indexPath.row;
				valueField.fieldType = cFieldType;
				valueField.autoresizingMask = nTableCellAutoresizingMask;
				valueField.numberOfLines = 99;
				valueField.tag = -2;
				[cell.contentView addSubview:valueField];
				[valueField release];
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			}
		}
		break;
			
		case '0': //html
		{
			valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
			UIWebView* webView = [[UIWebView alloc] initWithFrame:valueRect];
			webView.scalesPageToFit = NO;
			webView.tag = -2;
			webView.userInteractionEnabled = NO;
			[cell.contentView addSubview:webView];
			[webView release];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
		break;
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'C': //日期
		case 'D': //日期时间
			bDefault = NO;
		case '1': //公式
		case '2': //自动编号
		default:
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.numberOfLines = 99;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			if (readFlag || bDefault) 
				cell.accessoryType = UITableViewCellAccessoryNone;
			else
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
		break;
	}
	
	return cell;
}

+ (UITableViewCell *)createCellForViewForm:(UITableView *)tableView 
								   appItem:(NSMutableDictionary *)appItem
								 indexPath:(NSIndexPath *)indexPath
								  formView:(UIView *)formView
{
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSString *cellIdentifier = [NSString stringWithFormat:@"%@-view-cell-section=%d-row=%d-%@",fieldType,
								indexPath.section, indexPath.row, [MCUIToolkit getFieldName:appItem]];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
	if (cell) {
		return cell;
	}
	
	//CGRect cellFrame = CGRectMake(0, 0, 320, 40);
	cell = [[[MCTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
	
//	UIView* titleBackView = [[UIView alloc] initWithFrame:lableStyle==2? CGRectZero:CGRectMake(0, 0, lableStyle==1?305:100, 44)];
//	titleBackView.backgroundColor = [MCUserDefaults instance].viewCellTitleBackgroundColor;
//	[cell.contentView addSubview:titleBackView];
//	[titleBackView release];
	
	UILabel *labelField = [[UILabel alloc] initWithFrame:lableStyle==2?CGRectZero:CGRectMake(10, 4.0, lableStyle==1?300:95, 35)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:16];
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	if (style != nil) {
		float fontSize = 18.0;
		if ([style objectForKey:@"label_color"]) {
			labelField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"label_color"]]];
		}
		if ([style objectForKey:@"label_size"]) {
			fontSize = [[style objectForKey:@"label_size"] floatValue]/0.75;
			labelField.font = [UIFont boldSystemFontOfSize:fontSize];
		}
		if ([style objectForKey:@"label_style"]) {
			NSString *fontStyle = [style objectForKey:@"label_style"];
			if ([fontStyle isEqualToString:@"B"]) {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}else if ([fontStyle isEqualToString:@"I"]) {
				labelField.font = [UIFont italicSystemFontOfSize:fontSize];
			}else {
				labelField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
		}
	}
	labelField.numberOfLines = 1;
	labelField.tag = -1;
	[cell.contentView addSubview:labelField];
	[labelField release];
	
    //CGRect valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 9999);
	CGRect valueRect = CGRectMake(lableStyle>0?20:110, lableStyle==1?40:10, lableStyle>0?300:200, 9999);//huotianjun 
	
	if (cFieldType == 'O') {
		MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_TextField];
		valueField.textAlignment = UITextAlignmentLeft;
//		valueField.textColor = [UIColor grayColor];
		valueField.backgroundColor = [UIColor clearColor];
		valueField.font = [UIFont boldSystemFontOfSize:16];
		valueField.fieldType = cFieldType;
		valueField.tag = -2;
		valueField.autoresizingMask = nTableCellAutoresizingMask;
		valueField.secureTextEntry = YES;
		valueField.enabled = NO;
		[cell.contentView addSubview:valueField];
		[valueField release];
	} else if (cFieldType == 'S') {
        //valueRect = CGRectMake(lableStyle>0?10:110, lableStyle==1?40:10, lableStyle>0?300:200, 35);
		valueRect = CGRectMake(lableStyle>0?20:110, lableStyle==1?40:10, lableStyle>0?300:200, 35);
		MCImageView *imgView = [[MCImageView alloc] initWithFrame:valueRect];
		//imgView.autoresizingMask = nTableCellAutoresizingMask;
		imgView.tag = -2;
		[cell.contentView addSubview:imgView];
		[imgView release];
	} else if (cFieldType == '0') {
		valueRect.size.height = CGRectGetHeight(formView.frame) / 3;
		MCWebView* webView = [[MCWebView alloc] initWithFrame:valueRect];
		webView.scalesPageToFit = NO;
		webView.autoresizingMask = nTableCellAutoresizingMask;
		webView.tag = -2;
		webView.userInteractionEnabled = NO;
		webView.section = indexPath.section;
		webView.row = indexPath.row;
		[cell.contentView addSubview:webView];
		[webView release];
	} else {	
		MCControl *valueField = [[MCControl alloc] initWithFrame:valueRect withStyle:MCControl_Lable];
		valueField.textAlignment = UITextAlignmentLeft;
//		valueField.textColor = [UIColor grayColor];
		valueField.backgroundColor = [UIColor clearColor];
		valueField.font = [UIFont boldSystemFontOfSize:16];
		valueField.numberOfLines = 99;
		valueField.fieldType = cFieldType;
		valueField.tag = -2;
		valueField.autoresizingMask = nTableCellAutoresizingMask;
		[cell.contentView addSubview:valueField];
		[valueField release];
	}
	switch (cFieldType) {
		case 'S': //图片
		case 'J': //文件
		case 'T': //手机定位	
		case 'Y': //文件（多个）
		//case 'F': //文本
		case '9': //电话
		case 'A': //电子邮件
		case 'Q': //手机号码
		case '5': //超级链接
		case '8': //文本区
		case 'G': //文本区
		case '0': //html
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            //cell.accessoryType = UITableViewCellAccessoryNone;
			if (cFieldType == '8' || cFieldType == 'G' || cFieldType == 'S') {//huotianjun 增加了图片“S”也不要可选择深入打开
				cell.accessoryType = UITableViewCellAccessoryNone;
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
			}
			break;
		default:
			cell.accessoryType = UITableViewCellAccessoryNone;
			break;
	}
	
	return cell;
}

+ (void)filePreview:(UIViewController*)viewController
		   dataFrom:(NSString *)dataFrom
		   fileInfo:(NSMutableDictionary *)fileInfo {
	//NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
	NSString *fileName = [fileInfo objectForKey:@"fname"];
	if (fileName == nil || [fileName length] == 0)
		return;
	//if ([fileName hasSuffix:@"mov"]) {//huotianjun 注释掉
	//	[[MCErrorManagement instance] doError:@"99" returnMessage:@"视频文件暂不支持播放功能!"];
	//	return;
	//}
	
	NSString* szExtension = [[fileName pathExtension] lowercaseString];
	NSString *actionType = @"download";
	if([MCUserDefaults instance].isShowOpenOffice){
		if ([szExtension isEqualToString:@"rar"] || [szExtension isEqualToString:@"zip"]) {
			RarOrZipController *rarOrZipView = [[RarOrZipController alloc] initWithStyle:UITableViewStylePlain];
			[viewController.navigationController pushViewController:rarOrZipView animated:YES];
			rarOrZipView.title = fileName;
			
			NSMutableDictionary *fileParam = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											  fileName,@"fname",
											  [fileInfo objectForKey:@"lcn"],@"lcn",nil];
			
			[rarOrZipView getDataFromServer:dataFrom
								  fileParam:fileParam];
			[fileParam release];
			[rarOrZipView release];
			return;
		}
		
		actionType = @"preview";
//		if ([szExtension isEqualToString:@"doc"] || [szExtension isEqualToString:@"docx"] || [szExtension isEqualToString:@"txt"]) {
//			actionType = @"preview";
//		} 
	}

	NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
	NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										  fileName,@"fileName",
										  fileLocation,@"fileLocation",
										  actionType,@"actionType",
										  dataFrom,@"dataFrom",nil];
	if([MCUserDefaults instance].isShowOpenOffice){
		EGOPhotoViewController *fileView = [[EGOPhotoViewController alloc] init];
		[viewController.navigationController pushViewController:fileView animated:YES];
		fileView.title = @"文件下载中...";
		fileView.fileAttribute = fileAttribute;
		[fileAttribute release];
		[fileView getDataFromServer];
		[fileView release];
		
	} else {
		FileViewController *fileView = [[FileViewController alloc] init];
		[viewController.navigationController pushViewController:fileView animated:YES];
		fileView.title = @"文件下载中...";
		fileView.fileAttribute = fileAttribute;
		[fileAttribute release];
		[fileView getDataFromServer];
		[fileView release];
	}
}

+ (void)selectDetailForViewForm:(UIViewController*)viewController
					   withCell:(UITableViewCell *)aCell 
				  withIndexPath:(NSIndexPath*)aIndexPath
						appItem:(NSMutableDictionary *)appItem 
						   data:(NSMutableDictionary *)fieldData {
	
	NSMutableDictionary *field = [MCUIToolkit getField:appItem];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '3': //主－详细关系
		case '4': //查找关系
		case '6': //百分比
		case '7': //金额
		case 'E': //数字
		case 'B': //复选框
		case 'C': //日期
		case 'D': //日期时间
			break;
		case '5': //超级链接
		{
			if (nil != [fieldData objectForKey:@"value"] && [[fieldData objectForKey:@"value"] length] > 0) {
				[MCFunction goUrl:viewController withUrl:[fieldData objectForKey:@"value"]];
			}
		}
			break;
		case '9': //电话
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
                
                NSArray *teleArray = [[fieldData objectForKey:@"value"] componentsSeparatedByString:@"\\"];//huotianjun 增加，解决多个号码问题
                for (NSString * teleStr in teleArray) {//huotianjun 增加，解决多个号码问题
                    if (fieldAction.actionUrls == Nil)
                        fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
                                                  [NSString stringWithFormat:@"tel://%@",teleStr],
                                                  nil];
                    else{
                        [fieldAction.actionUrls addObject:[NSString stringWithFormat:@"tel://%@",teleStr]];
                    }
                }
                
                
				//fieldAction.actionUrls = [[NSMutableArray alloc] //huotianjun 注释掉 解决多个电话问题
				//						  initWithObjects:
				//						  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
				//						  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
                
                for (NSString * teleStr in teleArray) {//huotianjun 增加，解决多个号码问题
                    [actionSheet addButtonWithTitle:[NSString stringWithFormat:@"拨打电话:%@",teleStr]];                             
                }
                
				//[actionSheet addButtonWithTitle:@"拨打电话"];//huotianjun 注释掉，解决多个电话问题
                
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话!"];
			}
		}
			break;
		case 'A': //电子邮件
		{
			FieldAction *fieldAction = [[FieldAction alloc] init];
			fieldAction.viewController = viewController;
			fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
									  [NSString stringWithFormat:@"mailto:%@",[fieldData objectForKey:@"value"]],
									  nil];
			UIActionSheet *actionSheet = [[UIActionSheet alloc]
										  initWithTitle:@"请选择操作"
										  delegate:fieldAction
										  cancelButtonTitle:nil
										  destructiveButtonTitle:nil 
										  otherButtonTitles:nil];
			[actionSheet addButtonWithTitle:@"发送邮件"];
			int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
			actionSheet.cancelButtonIndex = cancelIndex;
			[actionSheet showInView:viewController.clientView.window];
			[actionSheet release];
			
		}
			break;
		case 'Q': //手机号码
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
                
                
                NSArray *teleArray = [[fieldData objectForKey:@"value"] componentsSeparatedByString:@"\\"];//huotianjun 增加，解决多个号码问题
                for (NSString * teleStr in teleArray) {//huotianjun 增加，解决多个号码问题
                    if (fieldAction.actionUrls == Nil)
                        fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
                                                  [NSString stringWithFormat:@"tel://%@",teleStr],
                                                  [NSString stringWithFormat:@"sms://%@",teleStr],
                                                  nil];
                    else{
                        [fieldAction.actionUrls addObject:[NSString stringWithFormat:@"tel://%@",teleStr]];
                        [fieldAction.actionUrls addObject:[NSString stringWithFormat:@"sms://%@",teleStr]];
                    }
                }
                
                
                
				//fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:   //huotianjun 注释掉
				//						  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
				//						  [NSString stringWithFormat:@"sms://%@",[fieldData objectForKey:@"value"]],
				//						  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
                for (NSString * teleStr in teleArray) {//huotianjun 增加，解决多个号码问题
                    [actionSheet addButtonWithTitle:[NSString stringWithFormat:@"拨打电话:%@",teleStr]];
                    [actionSheet addButtonWithTitle:[NSString stringWithFormat:@"发送短信:%@",teleStr]];                                    
                }

                //[actionSheet addButtonWithTitle:@"拨打电话"];//huotianjun 注释掉
				//[actionSheet addButtonWithTitle:@"发送短信"];//huotianjun 注释掉
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话和发短信!"];
			}
		}
			break;
			
		case 'F': //文本
		case '8': //文本区
		case 'G': //文本区
		{
			return;
			/*
			int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
			if (lableStyle == 1 || lableStyle == 2) {
				return;
			}
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			
			textFieldView.isReadOnly = YES;
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];*/
		}
			break;
			
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'O': //密码
			break;
			
		case 'S': //图片
		case 'J': //文件
		{
			NSString *dataFrom = [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
			NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
			if ([fileInfos count] != 1) {
				return;
			}
			NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
			
			[self filePreview:viewController
					 dataFrom:dataFrom
					 fileInfo:fileInfo];
			
			/*
			NSString *fileName = [fileInfo objectForKey:@"fname"];
			if (fileName == nil || [fileName length] == 0)
				return;
			if ([fileName hasSuffix:@"avi"]) {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"视频文件暂不支持播放功能!"];
				return;
			}
			
			NSString* szExtension = [[fileName pathExtension] lowercaseString];
			if ([szExtension isEqualToString:@"rar"] || [szExtension isEqualToString:@"zip"]) {
				RarOrZipController *rarOrZipView = [[RarOrZipController alloc] initWithStyle:UITableViewStylePlain];
				[viewController.navigationController pushViewController:rarOrZipView animated:YES];
				rarOrZipView.title = fileName;
				
				NSMutableDictionary *fileParam = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
													  fileName,@"fname",
													  [fileInfo objectForKey:@"lcn"],@"lcn",nil];
				
				[rarOrZipView getDataFromServer:dataFrom
									  fileParam:fileParam];
				[fileParam release];
				[rarOrZipView release];
				return;
			}
			
			
			NSString *actionType = @"download";
			if ([szExtension isEqualToString:@"doc"] || [szExtension isEqualToString:@"txt"]) {
				actionType = @"preview";
			} 
			
			NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
			NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												  fileName,@"fileName",
												  fileLocation,@"fileLocation",
												  actionType,@"actionType",
												  dataFrom,@"dataFrom",nil];
			FileViewController *fileView = [[FileViewController alloc] init];
			[viewController.navigationController pushViewController:fileView animated:YES];
			fileView.title = @"文件下载中...";
			fileView.fileAttribute = fileAttribute;
			[fileAttribute release];
			[fileView getDataFromServer];
			[fileView release];
			 */
			return;
		}
			break;
		case '0': //html
		{
			NSString* htmlString = [fieldData objectForKey:@"value"];
			MCWebViewController* htmlView = [[MCWebViewController alloc] init];
			[viewController.navigationController pushViewController:htmlView animated:YES];
			[htmlView loadHTMLString:htmlString];
			[htmlView release];
		}
			break;
		case 'T': //手机定位
		{
//			LocationView *locationView = [[LocationView alloc] init];
//			locationView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
//			
//			locationView.fieldData = fieldData;
//			locationView.field = field;
//			locationView.isViewMode = YES;
//			
//			[viewController.navigationController pushViewController:locationView animated:YES];
//			[locationView release];
		}
			break;
			
		case 'Y': //文件（多个）
		{
			if (nil == [fieldData objectForKey:@"fi"] || 0 == [[fieldData objectForKey:@"fi"] count]) {
				return;
			}
			FileListViewController *fileListView = [[FileListViewController alloc] initWithStyle:UITableViewStylePlain];
			fileListView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			
			fileListView.fieldData = fieldData;
			fileListView.field = field;
			fileListView.isViewMode = YES;
			
			[viewController.navigationController pushViewController:fileListView animated:YES];
			[fileListView release];
		}
			break;
			
		case 'M': //精确扫描
		case 'N': //简易扫描
		case 'P': //智能卡
		case 'R': //条形码
		case 'U': //多选过滤
		case 'V': //gps
		case 'W': //彩码
		case 'X': //流媒体
			break;
		default:
			break;
	}
}

+ (UITableViewCell *) createCellForQueryForm:(UITableView *)tableView 
									 appItem:(NSMutableDictionary *)appItem
								   indexPath:(NSIndexPath *)indexPath
									formView:(UIView *)formView {
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	NSString *cellIdentifier = [[NSString alloc] initWithFormat:@"%@-edit-cell-indexPath(section:%d,row:%d)-%@",fieldType,
								indexPath.section, indexPath.row, [MCUIToolkit getFieldName:appItem]];
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell) {
		return cell;
	}
	CGRect cellFrame = CGRectMake(0, 0, 500, 40);
	cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:cellIdentifier] autorelease];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	//UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 43)];
	//bg.backgroundColor = [UIColor whiteColor];
	//[cell addSubview:bg];
	//[cell sendSubviewToBack:bg];
	//[bg release];
	
	UILabel *labelField = [[UILabel alloc] initWithFrame:CGRectMake(10, 4.0, 75, 35)];
	labelField.textAlignment = UITextAlignmentLeft;
	labelField.textColor = [UIColor grayColor];//[UIColor blackColor];
	labelField.backgroundColor = [UIColor clearColor];
	labelField.font = [UIFont boldSystemFontOfSize:14];
	labelField.numberOfLines = 1;
	
	labelField.tag = -1;
	[cell.contentView addSubview:labelField];
	[labelField release];
	
	UILabel *modeLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 6.0, 32, 30)];
	modeLabel.textAlignment = UITextAlignmentRight;
	modeLabel.textColor = [UIColor colorWithRed:0.690 green:0.690 blue:0.690 alpha:1];
	modeLabel.backgroundColor = [UIColor clearColor];
	modeLabel.font = [UIFont boldSystemFontOfSize:12];
	modeLabel.numberOfLines = 1;
	
	modeLabel.tag = -3;
	[cell.contentView addSubview:modeLabel];
	[modeLabel release];
	
	BOOL bDefault = YES;
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '6': //百分比
		case '5': //超级链接
		case '7': //金额
		case '9': //电话
		case 'A': //电子邮件
		case 'E': //数字
		case 'F': //文本
		case 'Q': //手机号码
		case 'O': //密码
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:CGRectMake(130, 10, 160, 30) withStyle:MCControl_TextField];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			NSMutableDictionary *field = [appItem objectForKey:@"fld"];
			valueField.fieldName = [field objectForKey:@"name"];
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.enabled = YES;
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
			if (cFieldType == 'O') {
				valueField.secureTextEntry = YES;
			} else if (cFieldType == 'A') {
				valueField.keyboardType = UIKeyboardTypeEmailAddress;
			} else if (cFieldType == '5') {
				valueField.keyboardType = UIKeyboardTypeURL;
			} else if (cFieldType == '6' || cFieldType == '7' || cFieldType == 'E') {
				valueField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
			} else if (cFieldType == '9' || cFieldType == 'Q') {
				valueField.keyboardType = UIKeyboardTypePhonePad;
			} else {
				valueField.keyboardType = UIKeyboardTypeDefault;
			}
		}
			break;
		case 'B': //复选框
		{
			MCSwitch *valueField = [[MCSwitch alloc] initWithFrame:CGRectMake(200, 8, 110, 35)];
			valueField.fieldName = [MCUIToolkit getFieldName:appItem];
			valueField.tag = -2;
			cell.accessoryType = UITableViewCellAccessoryNone;
			[cell.contentView addSubview:valueField];
			[valueField release];
		}
			break;
		case 'Y': //文件（多个）
		case '8': //文本区
		case 'G': //文本区
		case 'T': //手机定位
		case '0': //html
		case 'J': //文件
		case 'S': //图片
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:CGRectMake(130, 10, 160, 30) withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.tag = -2;
			valueField.enabled = NO;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			[cell.contentView addSubview:valueField];
			[valueField release];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
			break;
		case '3': //主－详细关系
		case '4': //查找关系
		case 'U': //多选过滤
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'C': //日期
		case 'D': //日期时间
			bDefault = NO;
		default:
		{
			MCControl *valueField = [[MCControl alloc] initWithFrame:CGRectMake(130, 6, 160, 30) withStyle:MCControl_Lable];
			valueField.textAlignment = UITextAlignmentLeft;
//			valueField.textColor = [UIColor grayColor];
			valueField.backgroundColor = [UIColor clearColor];
			valueField.font = [UIFont boldSystemFontOfSize:16];
			valueField.tagEx = indexPath.section*100 +indexPath.row;
			valueField.fieldType = cFieldType;
			valueField.tag = -2;
			valueField.autoresizingMask = nTableCellAutoresizingMask;
			valueField.enabled = NO;
			[cell.contentView addSubview:valueField];
			[valueField release];
			
			if (bDefault) 
				cell.accessoryType = UITableViewCellAccessoryNone;
			else
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
			break;
	}
	return cell;
}

+ (void)selectDetailForQueryForm:(UIViewController*)viewController
						withCell:(UITableViewCell *)aCell 
						 appItem:(NSMutableDictionary *)appItem 
						  record:(NSMutableDictionary *)record {
	[MCFunction selectDetailForEditFormByAppItem:viewController withCell:aCell appItem:appItem record:record isQueryForm:YES];
}

+ (NSString *)StringFromDate:(NSDate *)aDate format:(NSString *)aFormat {
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
	[formatter setDateFormat:aFormat];
	NSString *dateString = [formatter stringFromDate:aDate];
	[formatter release];
	return dateString;
}

+(NSDate *)DateFromString:(NSString *)string format:(NSString *)aFormat{    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
    [formatter setDateFormat:aFormat];
    NSDate *date = [formatter dateFromString:string];
    [formatter release];
    return date;
}


+ (NSString *)getTempFileName:(NSString *)extName {
	int i = 1;
	NSString *uniquePath = [NSString stringWithFormat:@"%@%d.%@",NSTemporaryDirectory(),i,extName];
	while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath]) {
		i++;
		uniquePath = [NSString stringWithFormat:@"%@%d.%@",NSTemporaryDirectory(),i,extName];
	}
	return [NSString stringWithFormat:@"%d.%@",i,extName];
}

+ (NSString *)machine
{
	size_t size;
	
	// Set 'oldp' parameter to NULL to get the size of the data
	// returned so we can allocate appropriate amount of space
	sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
	
	// Allocate the space to store name
	char *name = malloc(size);
	
	// Get the platform name
	sysctlbyname("hw.machine", name, &size, NULL, 0);
	
	// Place name into a string
	NSString *machine = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
	
	// Done with this
	free(name);
	
	return machine;
}

+ (UIButton *)buttonWithTitle:	(NSString *)title
					   target:(id)target
					 selector:(SEL)selector
						frame:(CGRect)frame
						image:(UIImage *)image
				 imagePressed:(UIImage *)imagePressed
				darkTextColor:(BOOL)darkTextColor
{	
	UIButton *button = [[UIButton alloc] initWithFrame:frame];
	// or you can do this:
	//		UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
	//		button.frame = frame;
	
	button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	
	[button setTitle:title forState:UIControlStateNormal];
	if (darkTextColor)
	{
		[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	}
	else
	{
		[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	}
	button.titleLabel.font = [UIFont systemFontOfSize: 12];
	UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
	[button setBackgroundImage:newImage forState:UIControlStateNormal];
	
	UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
	[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
	
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
    // in case the parent view draws with a custom color or gradient, use a transparent color
	button.backgroundColor = [UIColor clearColor];
	
	return button;
}

+ (UIButton *)createHelpButton:(id)target
					  selector:(SEL)selector
						 frame:(CGRect)frame
{	
	//UIButton *button = [[UIButton alloc] initWithFrame:frame];
	// or you can do this:
	UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
	button.frame = frame;
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	return button;
}

+ (void)goUrl:(UIViewController*)viewController withUrl:(NSString *)urlString {
	urlString = [urlString lowercaseString];
	if (![urlString hasPrefix:@"http://"]) {
		urlString = [NSString stringWithFormat:@"http://%@",urlString];
	}
	
	MCWebViewController *helpController = [[MCWebViewController alloc] init];
	[viewController.navigationController pushViewController:helpController animated:YES];
	[helpController loadHTMLfromURL:urlString];
	[helpController release];
}

+ (UIImage *) scaleAndRotateImage:(UIImage*)photoimage 
							width:(CGFloat)bounds_width
						   height:(CGFloat)bounds_height {	
    CGImageRef imgRef =photoimage.CGImage;
	
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
	
	
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
	bounds.size.width = bounds_width;
    bounds.size.height = bounds_height;
	
    CGFloat scaleRatio = bounds.size.width / width;
    CGFloat scaleRatioheight = bounds.size.height / height;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient =photoimage.imageOrientation;
    switch(orient) {
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
			
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
			
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
			
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
			
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
			
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
			
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
			
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
			
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
            break;
    }
	
    UIGraphicsBeginImageContext(bounds.size);
	
    CGContextRef context = UIGraphicsGetCurrentContext();
	
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
        CGContextTranslateCTM(context, 0, -height);
    }
	
    CGContextConcatCTM(context, transform);
	
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

+ (NSString *)getQueryName:(NSString *)fieldName queryMode:(NSString *) queryMode {
	switch ([queryMode intValue]) {
		case 1:
			return fieldName;
			break;
		case 2:
			return [fieldName stringByAppendingString:@"_like"];
			break;
		case 3:
			return [fieldName stringByAppendingString:@"_min"];
			break;
		case 4:
			return [fieldName stringByAppendingString:@"_max"];
			break;
		case 5:
			return [fieldName stringByAppendingString:@"_in"];
			break;
		default:
			return fieldName;
			break;
	}

}

#pragma mark --- pushViewController ---
+ (void)pushViewController:(UIViewController *)viewController 
{
	MClientAppDelegate *delegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	UINavigationController *navController = [delegate navController];
	[navController pushViewController:viewController animated:YES];
	[viewController release];
}

//将十六进制的颜色值转换为uicolor:rrggbbhh
+ (UIColor*)hexColor:(NSString*)hexColor
{
	unsigned int red, green, blue, alpha;
	NSRange range;
	range.length = 2;
	
	range.location = 0; 
	[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
	range.location = 2; 
	[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
	range.location = 4; 
	[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue]; 
	range.location = 6; 
	[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&alpha];
	
	return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:(float)(alpha/100.0f)];
}

+ (void)saveScreen:(UIWindow*)window
{
	UIGraphicsBeginImageContext(window.frame.size);
	[window.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
#if TARGET_IPHONE_SIMULATOR
	NSString *filePath = @"/Users/shizhengye/Desktop/CopyScreen.png";
#else
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CopyScreen.png"];
#endif
	
	
	NSError *error;
	if([UIImagePNGRepresentation(image) writeToFile:filePath options:NSAtomicWrite error:&error]) {
		MCLog(@"%@ save ok!", filePath);
	} else {
		MCLog(@"%@ save failed, error:%@", filePath, [error description]);
	}
}

+ (NSString*)documentPath
{
#if TARGET_IPHONE_SIMULATOR
	NSFileManager *manager = [NSFileManager defaultManager];
	[manager createDirectoryAtPath:@"/Developer/Documentation/Iphone/" attributes:nil];
	return @"/Developer/Documentation/Iphone/";
#else
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSFileManager *manager = [NSFileManager defaultManager];
	NSString *mcPaths = [[paths objectAtIndex:0] stringByAppendingString:@"/MClient/"];
	[manager createDirectoryAtPath:mcPaths attributes:nil];

	return mcPaths;
#endif
}

+ (void)unzipFile:(NSString*)aZipFile withPath:(NSString*)aPath
{
	ZipArchive* zip = [[ZipArchive alloc] init];
	NSString *dcoumentpath = [MCFunction documentPath];
	NSString* l_zipfile = [dcoumentpath stringByAppendingString:aZipFile] ;
	NSString* unzipto = [dcoumentpath stringByAppendingString:aPath] ;
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	[fileManager removeItemAtPath:unzipto error:nil];
	
	if( [zip UnzipOpenFile:l_zipfile] )
	{
		BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
		if( NO==ret )
		{
		}
		[zip UnzipCloseFile];
	}
	[zip release];
}

+ (NSString *)getAppName {
	return [[MClientAppDelegate getInfoDictionary] objectForKey:@"CFBundleDisplayName"];
}


#pragma mark --- 多选回填列表，值操作函数 ---
//初始化选中
+ (NSMutableArray *)initSelectedValue:(NSString*)aValue {
	NSMutableArray *valueArray = [[NSMutableArray alloc] init];
	
	if (aValue != nil || [aValue isEqualToString:@""]) {
		NSMutableArray *values = [[NSMutableArray alloc] initWithArray:[aValue componentsSeparatedByString:@","]];
		for(int i = 0; i < [values count];i++) {
			if (i == 0 || i == [values count] - 1) {
				if ([[values objectAtIndex:i] isEqualToString:@""]) {
					continue;
				}
			}
			[valueArray addObject:[values objectAtIndex:i]];
		}
	}
	return valueArray;
}

//选中和不选中时更新
+ (void)updateSelectedValue:(NSMutableArray *)valueArray
					  value:(NSString*)aValue
				   selected:(bool)isSelect {
	if(isSelect) {
		if (aValue == nil || [aValue isEqualToString:@""]) {
			//[valueBufferArray addObject:@""];
			return;
		}
		
		for (int i = 0; i < [valueArray count]; i++) {
			NSString *value = [valueArray objectAtIndex:i];
			if([aValue isEqualToString:value]) {
				return;
			}
		}
		[valueArray addObject:aValue == nil?@"":aValue];
	} else {
		if (aValue == nil || [aValue isEqualToString:@""]) {
			return;
		}
		for (int i = 0; i < [valueArray count]; i++) {
			NSString *value = [valueArray objectAtIndex:i];
			if([aValue isEqualToString:value]){
				[valueArray removeObjectAtIndex:i];
				return;
			}
		}			  
	}
}

//获取选中结果
+ (NSString *)getSelectedValue:(NSMutableArray *)valueArray {
	NSString *result = @"";
	for(int i = 0;i<[valueArray count]; i++) {
		NSString *value = [valueArray objectAtIndex:i];
		if (value == nil || [value isEqualToString:@""]) {
			continue;
		}
		result = [result stringByAppendingFormat:@"%@,",value];
	}
	if ([result length] > 0) {
		result = [result substringToIndex:[result length] - 1];
	}
	return result;
}


@end
